package javacode.chapter06;

public class Encapsulation {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.name = "小明";
        teacher.age = -10;
        teacher.show();

        Teacher1 teacher1 = new Teacher1();
        teacher1.setName("小红");
        teacher1.setAge(-10);
        teacher1.show();
    }
}

class Teacher {
    String name;
    int age;

    public void show() {
        System.out.println("我叫" + name + "，今年" + age + "岁了！");
    }
}

class Teacher1 {
    private String name;
    private int age;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        if (age < 0) {
            this.age = 0;
        } else {
            this.age = age;
        }
    }

    public void show() {
        System.out.println("我叫" + this.name + "，今年" + this.age + "岁了！");
    }
}
